﻿namespace DiagnosticCenter.Web
{
    using System.Data;
    using System.Linq;
    using System.ServiceModel.DomainServices.EntityFramework;
    using System.ServiceModel.DomainServices.Hosting;


    // Implements application logic using the DiagcenterEntities context.
    // Also consider adding roles to restrict access as appropriate.
    // [RequiresAuthentication]
    [EnableClientAccess()]
    public class DatabaseService : LinqToEntitiesDomainService<DiagcenterEntities>
    {
        #region tblAnswers

        public IQueryable<tblAnswer> GetTblAnswers()
        {
            return this.ObjectContext.tblAnswers;
        }

        public void InsertTblAnswer(tblAnswer tblAnswer)
        {
            if ((tblAnswer.EntityState != EntityState.Detached))
            {
                this.ObjectContext.ObjectStateManager.ChangeObjectState(tblAnswer, EntityState.Added);
            }
            else
            {
                this.ObjectContext.tblAnswers.AddObject(tblAnswer);
            }
        }

        public void UpdateTblAnswer(tblAnswer currenttblAnswer)
        {
            this.ObjectContext.tblAnswers.AttachAsModified(currenttblAnswer, this.ChangeSet.GetOriginal(currenttblAnswer));
        }

        public void DeleteTblAnswer(tblAnswer tblAnswer)
        {
            if ((tblAnswer.EntityState != EntityState.Detached))
            {
                this.ObjectContext.ObjectStateManager.ChangeObjectState(tblAnswer, EntityState.Deleted);
            }
            else
            {
                this.ObjectContext.tblAnswers.Attach(tblAnswer);
                this.ObjectContext.tblAnswers.DeleteObject(tblAnswer);
            }
        }
        #endregion

        #region tblGroups

        public IQueryable<tblGroup> GetTblGroups()
        {
            return this.ObjectContext.tblGroups;
        }

        public void InsertTblGroup(tblGroup tblGroup)
        {
            if ((tblGroup.EntityState != EntityState.Detached))
            {
                this.ObjectContext.ObjectStateManager.ChangeObjectState(tblGroup, EntityState.Added);
            }
            else
            {
                this.ObjectContext.tblGroups.AddObject(tblGroup);
            }
        }

        public void UpdateTblGroup(tblGroup currenttblGroup)
        {
            this.ObjectContext.tblGroups.AttachAsModified(currenttblGroup, this.ChangeSet.GetOriginal(currenttblGroup));
        }

        public void DeleteTblGroup(tblGroup tblGroup)
        {
            if ((tblGroup.EntityState != EntityState.Detached))
            {
                this.ObjectContext.ObjectStateManager.ChangeObjectState(tblGroup, EntityState.Deleted);
            }
            else
            {
                this.ObjectContext.tblGroups.Attach(tblGroup);
                this.ObjectContext.tblGroups.DeleteObject(tblGroup);
            }
        }
        #endregion

        #region tblQuestions

        public IQueryable<tblQuestion> GetTblQuestions()
        {
            return this.ObjectContext.tblQuestions;
        }

        public IQueryable<tblQuestion> GetTblQuestionsSpecial()
        {
            return this.ObjectContext.tblQuestions;
        }

        public void InsertTblQuestion(tblQuestion tblQuestion)
        {
            if ((tblQuestion.EntityState != EntityState.Detached))
            {
                this.ObjectContext.ObjectStateManager.ChangeObjectState(tblQuestion, EntityState.Added);
            }
            else
            {
                this.ObjectContext.tblQuestions.AddObject(tblQuestion);
            }
        }

        public void UpdateTblQuestion(tblQuestion currenttblQuestion)
        {
            this.ObjectContext.tblQuestions.AttachAsModified(currenttblQuestion, this.ChangeSet.GetOriginal(currenttblQuestion));
        }

        public void DeleteTblQuestion(tblQuestion tblQuestion)
        {
            if ((tblQuestion.EntityState != EntityState.Detached))
            {
                this.ObjectContext.ObjectStateManager.ChangeObjectState(tblQuestion, EntityState.Deleted);
            }
            else
            {
                this.ObjectContext.tblQuestions.Attach(tblQuestion);
                this.ObjectContext.tblQuestions.DeleteObject(tblQuestion);
            }
        }
        #endregion

        #region tblResult

        public IQueryable<tblResult> GetTblResults()
        {
            return this.ObjectContext.tblResults;
        }

        public IQueryable<tblResult> GetTblResultsNested()
        {
            return this.ObjectContext.tblResults.Include("tblUser.tblGroup").Include("tblTest.tblSubject");
        }

        public IQueryable<tblResult> GetTblResultsTotal()
        {
            return this.ObjectContext.tblResults.Include("tblResultItems.tblQuestion.tblAnswers").Include("tblUser.tblGroup").Include("tblTest.tblSubject");
        }

        public void InsertTblResult(tblResult tblResult)
        {
            if ((tblResult.EntityState != EntityState.Detached))
            {
                this.ObjectContext.ObjectStateManager.ChangeObjectState(tblResult, EntityState.Added);
            }
            else
            {
                this.ObjectContext.tblResults.AddObject(tblResult);
            }
        }

        public void UpdateTblResult(tblResult currenttblResult)
        {
            this.ObjectContext.tblResults.AttachAsModified(currenttblResult, this.ChangeSet.GetOriginal(currenttblResult));
        }

        public void DeleteTblResult(tblResult tblResult)
        {
            if ((tblResult.EntityState != EntityState.Detached))
            {
                this.ObjectContext.ObjectStateManager.ChangeObjectState(tblResult, EntityState.Deleted);
            }
            else
            {
                this.ObjectContext.tblResults.Attach(tblResult);
                this.ObjectContext.tblResults.DeleteObject(tblResult);
            }
        }
        #endregion

        #region tblResultItem

        public IQueryable<tblResultItem> GetTblResultItems()
        {
            return this.ObjectContext.tblResultItems;
        }

        public void InsertTblResultItem(tblResultItem tblResultItem)
        {
            if ((tblResultItem.EntityState != EntityState.Detached))
            {
                this.ObjectContext.ObjectStateManager.ChangeObjectState(tblResultItem, EntityState.Added);
            }
            else
            {
                this.ObjectContext.tblResultItems.AddObject(tblResultItem);
            }
        }

        public void UpdateTblResultItem(tblResultItem currenttblResultItem)
        {
            this.ObjectContext.tblResultItems.AttachAsModified(currenttblResultItem, this.ChangeSet.GetOriginal(currenttblResultItem));
        }

        public void DeleteTblResultItem(tblResultItem tblResultItem)
        {
            if ((tblResultItem.EntityState != EntityState.Detached))
            {
                this.ObjectContext.ObjectStateManager.ChangeObjectState(tblResultItem, EntityState.Deleted);
            }
            else
            {
                this.ObjectContext.tblResultItems.Attach(tblResultItem);
                this.ObjectContext.tblResultItems.DeleteObject(tblResultItem);
            }
        }
        #endregion

        #region tblSubjects

        public IQueryable<tblSubject> GetTblSubjects()
        {
            return this.ObjectContext.tblSubjects;
        }

        public void InsertTblSubject(tblSubject tblSubject)
        {
            if ((tblSubject.EntityState != EntityState.Detached))
            {
                this.ObjectContext.ObjectStateManager.ChangeObjectState(tblSubject, EntityState.Added);
            }
            else
            {
                this.ObjectContext.tblSubjects.AddObject(tblSubject);
            }
        }

        public void UpdateTblSubject(tblSubject currenttblSubject)
        {
            this.ObjectContext.tblSubjects.AttachAsModified(currenttblSubject, this.ChangeSet.GetOriginal(currenttblSubject));
        }

        public void DeleteTblSubject(tblSubject tblSubject)
        {
            if ((tblSubject.EntityState != EntityState.Detached))
            {
                this.ObjectContext.ObjectStateManager.ChangeObjectState(tblSubject, EntityState.Deleted);
            }
            else
            {
                this.ObjectContext.tblSubjects.Attach(tblSubject);
                this.ObjectContext.tblSubjects.DeleteObject(tblSubject);
            }
        }
        #endregion

        #region tblTests
        public IQueryable<tblTest> GetTblTests()
        {
            return this.ObjectContext.tblTests.Include("tblSubject");
        }

        public IQueryable<tblTest> GetTblTestTotal()
        {
            return this.ObjectContext.tblTests.Include("tblQuestions.tblAnswers");
        }

        public IQueryable<tblTest> GetTblTestsSpecial(int testid)
        {
            return this.ObjectContext.tblTests.Where(param => param.TestID == testid);
        }

        public void InsertTblTest(tblTest tblTest)
        {
            if ((tblTest.EntityState != EntityState.Detached))
            {
                this.ObjectContext.ObjectStateManager.ChangeObjectState(tblTest, EntityState.Added);
            }
            else
            {
                this.ObjectContext.tblTests.AddObject(tblTest);
            }
        }

        public void UpdateTblTest(tblTest currenttblTest)
        {
            this.ObjectContext.tblTests.AttachAsModified(currenttblTest, this.ChangeSet.GetOriginal(currenttblTest));
        }

        public void DeleteTblTest(tblTest tblTest)
        {
            if ((tblTest.EntityState != EntityState.Detached))
            {
                this.ObjectContext.ObjectStateManager.ChangeObjectState(tblTest, EntityState.Deleted);
            }
            else
            {
                this.ObjectContext.tblTests.Attach(tblTest);
                this.ObjectContext.tblTests.DeleteObject(tblTest);
            }
        }
        #endregion

        #region tblTestSchedules

        public IQueryable<tblTestSchedule> GetTblTestSchedules()
        {
            return this.ObjectContext.tblTestSchedules.Include("tblTest.tblSubject").Include("tblGroup");
        }

        public void InsertTblTestSchedule(tblTestSchedule tblTestSchedule)
        {
            if ((tblTestSchedule.EntityState != EntityState.Detached))
            {
                this.ObjectContext.ObjectStateManager.ChangeObjectState(tblTestSchedule, EntityState.Added);
            }
            else
            {
                this.ObjectContext.tblTestSchedules.AddObject(tblTestSchedule);
            }
        }

        public void UpdateTblTestSchedule(tblTestSchedule currenttblTestSchedule)
        {
            this.ObjectContext.tblTestSchedules.AttachAsModified(currenttblTestSchedule, this.ChangeSet.GetOriginal(currenttblTestSchedule));
        }

        public void DeleteTblTestSchedule(tblTestSchedule tblTestSchedule)
        {
            if ((tblTestSchedule.EntityState != EntityState.Detached))
            {
                this.ObjectContext.ObjectStateManager.ChangeObjectState(tblTestSchedule, EntityState.Deleted);
            }
            else
            {
                this.ObjectContext.tblTestSchedules.Attach(tblTestSchedule);
                this.ObjectContext.tblTestSchedules.DeleteObject(tblTestSchedule);
            }
        }
        #endregion

        #region tblUsers
        public IQueryable<tblUser> GetTblUsers()
        {
            return this.ObjectContext.tblUsers;
        }

        public IQueryable<tblUser> GetSpecialUsers(int groupid)
        {
            return this.ObjectContext.tblUsers.Include("tblGroup").Where(param => param.GroupID == groupid).OrderBy(e => e.LastName);
        }

        public IQueryable<tblUser> GetTblUsersRole()
        {
            return ObjectContext.tblUsers.Include("tblGroup")
                .Where(param => param.Role == "3")
                .OrderBy(e => e.LastName);;
        }

        public void InsertTblUser(tblUser tblUser)
        {
            if ((tblUser.EntityState != EntityState.Detached))
            {
                this.ObjectContext.ObjectStateManager.ChangeObjectState(tblUser, EntityState.Added);
            }
            else
            {
                this.ObjectContext.tblUsers.AddObject(tblUser);
            }
        }

        public void UpdateTblUser(tblUser currenttblUser)
        {
            this.ObjectContext.tblUsers.AttachAsModified(currenttblUser, this.ChangeSet.GetOriginal(currenttblUser));
        }

        public void DeleteTblUser(tblUser tblUser)
        {
            if ((tblUser.EntityState != EntityState.Detached))
            {
                this.ObjectContext.ObjectStateManager.ChangeObjectState(tblUser, EntityState.Deleted);
            }
            else
            {
                this.ObjectContext.tblUsers.Attach(tblUser);
                this.ObjectContext.tblUsers.DeleteObject(tblUser);
            }
        }
        #endregion
    }
}